<?php

session_start();
//error_reporting(0);
if(!isset($_SESSION['userId']))
{
	header("location:index.php");
}
include("../class/config.php");
include("../class/cls_faq.php");

if(isset($_POST['send'])=="add" && $_POST!=NULL)
{
	$faq_question=$_POST['faq_question']; 
	$faq_answer=$_POST['faq_Answer'];
	$faq_status=isset($_POST['faq_status'])?"1":"0";
	
	if(($_GET['action'])=='add')
	{
		if (!$_POST['faq_question']=="" &&!$_POST['faq_Answer']=="") {
			
		
		$result= $objFaq -> insertFaq($faq_question,$faq_answer,$faq_status);
		}
		
	} 
	else if(($_GET['action'])=='edit'){
		
		$faq_id= $_GET['editId'];
		if (!$_POST['faq_question']=="" &&!$_POST['faq_Answer']==""){
			$result= $objFaq -> updateFaq($faq_id,$faq_question,$faq_answer,$faq_status);
			if($result){
			header("location:faq.php");
			}
		}
		
	}
		if(isset($result)){
			$success= "<span class='successmsg'>processed completed</span>";}
		if(!isset($result)){
			$success="<span class='errormsg'>processed failed !!</span>";
		}
	
}
	if(isset($_GET['deleteId']))
	{
		$faqId=$_GET['deleteId'];
		$objFaq->deleteFaq($faqId);
		}
	if(isset($_GET['status'])){
			
			$faq_id= $_GET['faqId'];
			
			$status=$_GET['status'];
			
			$result=$objFaq -> updateStatus($faq_id,$status);
			
		}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FAQ's</title>
<link rel="stylesheet" href="../css/layout.css" />
</head>
<body>

	<div class="mainWrapper">
     <?php include("../includes/adminHeader.php");?>
    
    
    <div class="bodyContent">
    <h1>Faq</h1>
    <div class="innerBody">
    <?php if(!isset($_GET['action']))
	{?>
    <strong>FAQ Listing</strong>  | <a href="?action=add">Add FAQ</a>
     
	
      <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
        <tr>
          <th>Sn</th>
          <th>Faq Question</th>
          <th>Faq Answer</th>
          <th>Faq Status</th>
          <th>Action</th>
        </tr>
		<?php $resFaq = $objFaq->selectFaq();
		$i=1;
		
		while($recFaq=mysqli_fetch_array($resFaq))
		{
		?>
        <tr>
          <td><?php echo $i++;?></td>
          <td><?php echo $recFaq['faq_question'];?></td>
          <td><?php echo $recFaq['faq_answer'];?></td>
		  <td><?php
		   
		   if($recFaq['faq_status']==1){ ?>
		  <a href="?status=0&faqId=<?php echo $recFaq['faq_id']?>" >	<img src="../images/status1.jpg" /> </a>
		  	 <?php }
			
			else if($recFaq['faq_status']==0) { ?>
				
		  <a href="?status=1&faqId=<?php echo $recFaq['faq_id']?>" >	<img src="../images/status2.jpg" /> </a>
		  	 <?php } ?>
		  	  </td>
		  
          <td><a href="?action=edit&editId=<?php echo $recFaq['faq_id'];?>">Edit</a> | 
          		<a href="?deleteId=<?php echo $recFaq['faq_id'];?>"
          			 onclick="javascript:return confirm('Are You Sure You Want to Delete this Record?')">Delete</a>
			<a href="?editId=<?php echo $recFaq['faq_id']?>"></a></td>
        </tr>
        <?php } /// end of while($recContact=mysqli_fetch_array($resContact))
?>
       </table>
       
     <?php }
	  else if(isset($_GET['action']) && $_GET['action']=='add'or (isset($_GET['action']) && $_GET['action']=='edit') )
	 {
	 
	 ?>
      <strong><?php echo $_GET['action']=="add"?"Add Faq":"Edit Faq" ?></strong>
      <?php 
	  if(isset($success))
      {
      echo $success;
      } ?>
      <form id="form1" name="form1" method="post" action="">
        <table width="100%" border="0" class="table" cellpadding="1" cellspacing="1" >
        	
        	<?php
        	 
        	 if(($_GET['action'])=='edit'){
        	 	
			 $faqId=$_GET['editId'];
				 
        	 $res=$objFaq-> getFaqById($faqId);
        	 $result=(mysqli_fetch_array($res));
        	 }?>
          <tr>
            <td width="30%">FAQ Question *</td>
            <td><input type="text" name="faq_question" id="faq_question" class="textBox"
            	 value="<?php if(($_GET['action'])=='edit') echo $result["faq_question"];?>"/>
            	<?php if(isset($_POST['faq_question']) && $_POST['faq_question']==''){
            		echo "<span class='errormsg'>Question field is empty !</span>";
            	}?>
            	 </td>
          </tr>
          <tr>
            <td width="30%">Faq Answer *</td>
            <td><input type="text" name="faq_Answer" id="faq_Answer" class="textBox"
            	 value="<?php if(($_GET['action'])=='edit')echo $result["faq_answer"];?>"/>
            	 <?php if(isset($_POST['faq_Answer']) && $_POST['faq_Answer']==''){
            		echo "<span class='errormsg'>Answer field is empty !</span>";
            	}?>
            	 </td>
          </tr>
          <tr>
            <td width="30%">Faq Status *</td>
            <td><input type="checkbox" name="faq_status" id="faq_status"value="<?php if(($_GET['action'])=='edit')echo $result["faq_status"];?>"/></td>
          </tr>
        	<td>&nbsp;</td>
            <td><input type="submit" name="send" id="add" value="<?php echo $_GET['action']=="add"?"  Add  ":"  Edit  "?> "  />
            	<input type="button" name="cancel" value="cancel" onclick="javascript:location.href='faq.php'" /></td>
          </tr>
         <?php 
         			
		}?>
        </form>

       <?php
	  
      {?>
     
 
<?php }?>

		
    </div>
    
   </div>
    
    
    <?php //include("../includes/adminFooter.php");?>
     
 </div>

</body>
</html>